package cn.tedu.egmvc2.controller;

import cn.tedu.egmvc2.mapper.NoticeMapper;
import cn.tedu.egmvc2.pojo.dto.NoticeAddParam;
import cn.tedu.egmvc2.pojo.dto.NoticeDetailInfoQuery;
import cn.tedu.egmvc2.pojo.dto.NoticeListQuery;
import cn.tedu.egmvc2.pojo.entity.Notice;
import cn.tedu.egmvc2.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egmvc2.pojo.vo.NoticeListVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String NoticeAdd(NoticeAddParam noticeAddParam){
        System.out.println("noticeAddParam"+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return "新增公告成功";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> NoticeList(NoticeListQuery noticeListQuery){
        System.out.println("noticeListQuery"+noticeListQuery);
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNotice(title, type);
        return noticeListVOS;
    }

    @GetMapping("/v1/notice/detail")
    @ResponseBody
        public NoticeDetailInfoVO noticeDetailInfoVO(Long id){
            System.out.println("id = "+ id);
            NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
            return noticeDetailInfoVO;
        }
}
